package com.tech.view.component.button;

import com.tech.util.LogProcessUtil;
import com.tech.view.common.GraphicsUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.view.common.SwingTestUtils;
import com.tech.view.panel.GoodsSendPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * 提供图片上传功能的按钮
 */
public class ImageUploadButton extends SimpleIconButton implements ActionListener {
	private final static Logger LOG = LogProcessUtil.getLogger(ImageUploadButton.class.getName());
	private static BufferedImage DEFAULT_IMAGE = ResourcesUtil.getImage(ResourcesUtil.Image.SEND_GOODS_IMAGE);
	private int buttonSize; //长宽一样
	/**
	 * 图片更换次数，这个类里面没有使用，但是其他类可以用于一些判断，比如是否选择图片或者是否更改过图片
	 */
	private int selectCount = 0;
	private JFileChooser fileChooser;
	private String imagePath;
	private String suffix;
	private Runnable task; //存储选了一个图片后执行的操作，不过这里是单线程执行

	public ImageUploadButton() {
		this(104);
	}

	public ImageUploadButton(int buttonSize) {
		this.buttonSize = buttonSize;

		init();
	}

	public static void main(String[] args) {
		SwingTestUtils.loadSkin();
		ImageUploadButton uploadButton = new ImageUploadButton();
		JButton button = new JButton("获取");

		button.addActionListener(e -> {
			System.out.println(uploadButton.getImagePath());
			System.out.println(uploadButton.getSuffix());
		});

		SwingTestUtils.test(uploadButton, button);
	}

	private void init() {
		setBorder(null);

		// 图像缩放
		image = DEFAULT_IMAGE;
		if (buttonSize > 0) {
			image = GraphicsUtil.createScaledCompositeInstance(image, buttonSize, buttonSize);
			DEFAULT_IMAGE = image;
		}
		setPreferredSize(new Dimension(buttonSize, buttonSize));
		setImage(image);
		fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new FileNameExtensionFilter("只支持png、jpg图片", "jpg", "png"));

		addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		int result = fileChooser.showOpenDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) {
			selectCount++;
			imagePath = fileChooser.getSelectedFile().getPath();
			suffix = imagePath.substring(imagePath.lastIndexOf(".") + 1);
			try {
				setImage(ImageIO.read(new File(imagePath)));
				if (task != null)
					task.run(); //执行该方法
			} catch (IOException ex) {
				LOG.throwing(GoodsSendPanel.class.getName(), "为按钮设置商品图片", ex);
			}
		}
	}

	public String getImagePath() {
		return imagePath;
	}

	public String getSuffix() {
		return suffix;
	}

	public Runnable getTask() {
		return task;
	}

	/**
	 * 设置任务，不同于点击按钮，该任务用于选择图片后执行的操作
	 * 这里没有使用多线程，也没有使用集合存储任务
	 *
	 * @param task 选择图片后执行的操作
	 */
	public void setTask(Runnable task) {
		this.task = task;
	}

	@Override
	public BufferedImage getImage() {
		return image == DEFAULT_IMAGE ? null : image;
	}

	public int getSelectCount() {
		return selectCount;
	}
}
